import json
import pymysql
import re
import time
from lxml import etree
from requests import Request, Session
import requests
import demjson
from requests.exceptions import RequestException
import sys
from imp import reload
reload(sys)
import types
def get_one_page(url):
    try:
        s = Session()
        req = Request('GET', url)
        prepped = s.prepare_request(req)
        r = s.send(prepped)

        if r.status_code == 200:
            print(r.text)
            # jsonobj=eval("'{}'".format(r.text))
            jsonobj = eval(format(r.text))
            # res=json.loads(r.text.decode('utf-8').replace("'", "\""))
            return jsonobj
        return None
    except RequestException:
        return None

def dict_get(dict, objkey, default):
    tmp = dict
    for k,v in tmp.items():
        if k == objkey:
            return v
        else:
            if type(v) is types.DictType:
                ret = dict_get(v, objkey, default)
                if ret is not default:
                    return ret
    return default


def main():
    # 连接数据库
    db = pymysql.connect(host='localhost', user='root', password='', port=3306, db='spiders', charset='utf8')
    cursor = db.cursor()
    sql = 'select wid from louis_account  where proportion is NULL '

    cursor.execute(sql)
    # 获取所有记录列表
    results = cursor.fetchall()
    print(len(results))
    for (row,) in results:
        time.sleep(0.3)
        url = 'http://www.topklout.com/weizx/ReadData?uid='+str(row)

        text = get_one_page(url)

        text2 = demjson.decode(text.strip('"'))
        male_count=0
        male_precent=0
        female_count=0
        female_precent=0

        if "total_fans_age_data" in text:
            proportion = text2['data']['total_fans_age_data']
            # print(proportion)
            for k,v  in  proportion.items():
                male_count+=v['male_count']
                female_count += v['female_count']

            if male_count==0:
                male_precent = 0
            elif female_count==0:

                female_precent=0
            else:
                male_precent=male_count/(male_count+female_count)*10
                female_precent = female_count / (male_count + female_count)*10

            finallyData=str(round(male_precent))+':'+str(round(female_precent))
            print(finallyData)

            # 阅读中位数
            INSERT_sql = 'update louis_account set proportion=%s  where wid=%s'
            cursor.execute(INSERT_sql, (finallyData, row))
            db.commit()
        else:
            print('没有')







if __name__ == '__main__':
   main()
